import React, { Component, PropTypes } from 'react'

import './../css/default-checkbox.css'

class CheckboxInput extends Component {

	constructor (props, context) {
	    super(props, context)
	    const {checkboxClass, isCheck} = this.props
	    this.state = {isCheck: this.props.isCheck } // 初始化状态
	}

	componentWillMount() {
		//this.setState({isCheck: this.props.isCheck})
		console.log("select will")
	}

	componentDidMount() {
		console.log("select done")
	}

	componentWillReceiveProps(nextProps) {
		this.setState({isCheck: nextProps.isCheck})
	}

    // shouldComponentUpdate(nextProps, nextState) {
    // 	return nextProps.isCheck !== this.props.isCheck
    // }

	handleChangeChk(e) {
		var isCheck = this.state.isCheck ? false : true;
		this.setState({isCheck: isCheck});
		this.props.onChange(isCheck)
	}

	render() {
		//this.setState({isCheck: this.state.isCheck ? false : true})
		
		return (
			<input type="checkbox" id={this.props.id} style={this.props.style}
					checked={this.state.isCheck} disabled={this.props.disabled ? true : false}
					className={this.props.checkboxClass ? this.props.checkboxClass : "default-checkbox "}
					onChange={(e) => this.handleChangeChk(e)}/>
		)
	}
}
CheckboxInput.propTypes = {
	onChange: PropTypes.func.isRequired,
	isCheck : PropTypes.bool.isRequired
};

export default CheckboxInput
